package com.qingyun.edusysteam.controller;

import com.qingyun.edusysteam.dto.Result;
import com.qingyun.edusysteam.service.IScheduleService;
import com.qingyun.edusysteam.service.IStudentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingyun.edusysteam.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * <p>
 * 教师课表安排 前端控制器
 * </p>
 *
 * @author your_name
 * @since 2025-08-23
 */
@RestController
@RequestMapping("/schedule")
public class ScheduleController {
    
    @Autowired
    private IScheduleService scheduleService;
    @Autowired
    private IStudentService studentService;
    
    @GetMapping("/querySchedule")
    public Result querySchedule(
            @RequestParam("teacherId") Long teacherId, 
            @RequestParam("date") String dateStr,
            @RequestHeader("Authorization") String token) {
        
        // 参数验证
        if (teacherId == null) {
            return Result.fail("教师ID不能为空");
        }
        if (dateStr == null || dateStr.trim().isEmpty()) {
            return Result.fail("查询日期不能为空");
        }
        
        return scheduleService.querySchedule(teacherId, dateStr);
    }

    @GetMapping("/queryScheduleToFeedback")
    public Result queryScheduleToFeedback(@RequestParam("teacherId") Long teacherId,
                                          @RequestParam("date") String dateStr,
                                          @RequestHeader("Authorization") String token){
        // 参数验证
        if (teacherId == null) {
            return Result.fail("教师ID不能为空");
        }
        if (dateStr == null || dateStr.trim().isEmpty()) {
            return Result.fail("查询日期不能为空");
        }

        return scheduleService.queryScheduleToFeedback(teacherId, dateStr);
    }
    @GetMapping("/queryStudentSchedule")
    public Result queryScheduleByStudent(
            @RequestParam(value = "teacherId", required = false) Long teacherId,
            @RequestParam(value = "date", required = false) String dateStr,
            @RequestParam(value = "studentId") Long studentId,
            @RequestHeader("Authorization") String token) {

        // 参数验证
        if(studentId==null){
            return Result.fail("学生ID不能为空");
        }
        return scheduleService.queryStudentSchedule(teacherId, dateStr, studentId);
    }

    @GetMapping("/queryStudentScheduleByName")
    public Result queryScheduleByStudentName(
            @RequestParam("studentName") String studentName,
            @RequestParam(value = "teacherId", required = false) Long teacherId,
            @RequestParam(value = "date", required = false) String dateStr) {
        if (studentName == null || studentName.trim().isEmpty()) {
            return Result.fail("学生姓名不能为空");
        }
        Student student = studentService.getOne(new LambdaQueryWrapper<Student>()
                .eq(Student::getName, studentName).last("limit 1"));
        if (student == null) {
            return Result.fail("未找到该学生");
        }
        return scheduleService.queryStudentSchedule(teacherId, dateStr, student.getId());
    }
}
